package ru.urfc.portal.ui.client.googlets;

import java.util.ArrayList;
import java.util.Date;

import ru.urfc.portal.ui.client.BaseGooglet;
import ru.urfc.portal.ui.client.descriptor.GridDescriptor;
import ru.urfc.portal.ui.client.descriptor.GridField;
import ru.urfc.portal.ui.client.googlets.client.NetDeviceList;
import ru.urfc.portal.ui.client.model.catalogs.DeviceState;
import ru.urfc.portal.ui.client.widgets.MegaGrid;

import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.BasePagingLoader;
import com.extjs.gxt.ui.client.data.MemoryProxy;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoader;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Widget;

public class Sample extends BaseGooglet {

	interface Binder extends UiBinder<Widget, Sample> {}
	private static Binder binder = GWT.create(Binder.class);

	@UiField ComboBox<ModelData> comboBox;
	@UiField DateField dateField;
	@UiField TextField<String> textField;
	@UiField NumberField numberField;
	@UiField MegaGrid grid;
	@UiField(provided = true) PagingToolBar paging = new PagingToolBar(50); 
	
	@Override
	protected void init() {
		initWidget(binder.createAndBindUi(this));
		ListStore<ModelData> store = new ListStore<ModelData>();
		store.add(createModelData("value", "Значение 1"));
		store.add(createModelData("value", "Значение 2"));
		store.add(createModelData("value", "Значение 3"));
		comboBox.setStore(store);		
		comboBox.setDisplayField("value");
		comboBox.setEmptyText("Ничего на выбрано");
		comboBox.setTriggerAction(TriggerAction.ALL);
		
		dateField.setValue(new Date());
		textField.setValue("Текст");
		numberField.setValue(123L);
		
		GridDescriptor descriptor = new GridDescriptor("device", CLIENTS, getClientId(), DEVICES);
		GridField autoExpandColumnField = NetDeviceList.createColumns(descriptor);
		descriptor.addCIColumn("state/text", "state/code", DeviceState.class, "Статус", 100);
		
		createController(descriptor, grid);
        grid.setAutoExpandColumn(autoExpandColumnField.getName());
        grid.setWidth(400);
        
        ArrayList<ModelData> data = new ArrayList<ModelData>();
        for (int i = 0; i < 100; ++i) {
        	data.add(new BaseModelData());
        }
		final BasePagingLoadResult<ModelData> result = new BasePagingLoadResult<ModelData>(data, 50, data.size());
		MemoryProxy<ModelData> proxy = new MemoryProxy<ModelData>(result);
        PagingLoader<PagingLoadResult<ModelData>> loader = new BasePagingLoader<PagingLoadResult<ModelData>>(proxy) {
        	@Override
        	public boolean load(Object loadConfig) {
        		PagingLoadConfig pagingLoadConfig = (PagingLoadConfig) loadConfig;
        		result.setOffset(pagingLoadConfig.getOffset());
        		return super.load(loadConfig);
        	}
        };
        paging.bind(loader);
        paging.setWidth(400);
        loader.load(50, data.size());
	}

	private ModelData createModelData(String property, String value) {
		ModelData  model = new BaseModel();
		model.set(property, value);
		return model;
	}

}
